In this lesson, you will learn how to use the foreach loop in PHP. The foreach constructs is an easy way to traverse all elements of an array.
Introduction to foreach loop in PHP
First, let’s start off with the syntax. PHP foreach can be written in 2 ways:
foreach ( array_expression as $value) The statement
And
foreach (array_expression as $key => $value) The statement
The first variant loops across the array ‘array_expression’. PHP will assign the value of the current array element to the variable $value every iteration. And the internal pointer of an array moves to the next item which you will get in the next iteration.
The second variant of the foreach loop assigns the current element’s key to $key variable and value to the $value variable. Just like the first variant, the internal pointer will advance to the next item in the array until it reaches the end of the array.
In case you want to modify array elements within the loop, precede $value with address operator (&). The value will be assigned by reference in such scenario.
Example of foreach loop in PHP
This example uses the value-only variant of the foreach loop.
$arr = array(a, b, c, d, e, f, g); foreach ($arr as $value) { echo "Current value of $arr : $value.\n"; }
Foreach loop for an associative array
To iterative over elements of an associative array, you should use the second variant of the foreach loop.
Example of foreach loop with associative array
This example illustrates the usage of a key-value variant of the foreach loop in PHP.
$arr =array ("Los Angeles" => "CA", "New York City" => "NY", "Boston" => "MA"); foreach($arr as $key => $value){ echo $key .", ". $value."<br/>"; }
The output
Los Angeles, CA
New York City, NY
Boston, MA
<?php foreach ($arr as $value): #Your code goes here endforeach; ?>
Example of foreach loop and var_dump function
<?php $arr =array ("Los Angeles" => " CA", " New York City" => " NY", " Boston" => " MA"); echo "<pre>"; var_dump($arr); echo "</pre>"; ?>
The output
array(3) {
[“Los Angeles”]=>
string(2) “CA”
[“New York City “]=>
string(2) “NY”
[“Boston “]=>
string(2) “MA”
}
Exit a foreach loop
Use the break control statement to exit from the foreach loop.
Example of exiting a foreach loop
<?php $array = [ ' Los Angeles ', ' New York City ', ' Boston ', 'San Diego' ]; foreach( $array as $value ){ if( $value == ‘Boston’ ){ echo "City was found"; break; } } ?>
Foreach for multidimensional array
You can also iterate over a multidimensional array using the foreach loop.
Example I of foreach loop for multidimensional array
<?php $a = array(); $a[0][0] = "a"; $a[0][1] = "b"; $a[1][0] = "y"; $a[1][1] = "z"; foreach ($a as $v1) { foreach ($v1 as $v2) { echo "$v2\n"; } } ?>
Example II of foreach loop for multidimensional array
<?php $array = [ [1, 2, array(3, 4)], [3, 4, array(5, 6)], ]; foreach ($array as list($a, $b, list($c, $d))) { echo "A: $a; B: $b; C: $c; D: $d;<br>"; }; ?>
The output
A: 1; B: 2; C: 3; D: 4;
A: 3; B: 4; C: 5; D: 6;
PHP while loop | Tutorial Home | PHP Array |